home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / os2tools / bnklysrc / find.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-08  |  6.4 KB  |  249 lines

  1. #include <stdio.h>
  2. #include <dos.h>
  3. #include <string.h>
  4. #include <direct.h>
  5. #include <malloc.h>
  6. #include "find.h"
  7.  
  8. /*  <*********************************************************************>  */
  9. /*  <* find.c                                 04/06/88 Peter Fitzsimmons *>  */
  10. /*  <*********************************************************************>  */
  11. /*  <*                                                                   *>  */
  12. /*  <*                                                                   *>  */
  13. /*  <*   Compiler: Microsoft C Version 5.00, any memory model.           *>  */
  14. /*  <*         or: IBM C/2 Version 1.00, any memory model                *>  */
  15. /*  <*                                                                   *>  */
  16. /*  <*********************************************************************>  */
  17. /*  <* Notes: #define OS_2 for os/2                                      *>  */
  18. /*  <*        - Right now, this code is either for DOS or OS/2, not      *>  */
  19. /*  <*          both. ( ie: You can't use it for a Family app )          *>  */
  20. /*  <*********************************************************************>  */
  21. /*  <*  This module Copyright (C) 1988, 1989 Peter Fitzsimmons.          *>  */
  22. /*  <*  All rights reserved.                                             *>  */
  23. /*  <*********************************************************************>  */
  24.  
  25. /* findtime() : this func writen just for Binkley, which combines the
  26. file date and file time into one long in certain situations.
  27. */
  28. long findtime(FSCAN *dh)
  29. {
  30.     return( (long)dh->wr_date * 0x10000L + (long)dh->wr_time );
  31. }
  32.  
  33.  
  34. #ifndef OS_2     /* if not OS_2 assume MSC 5.00, for dos-only program */
  35.  
  36. FSCAN *opendir(void)
  37. {
  38.    FSCAN *retbuf;
  39.  
  40.    retbuf = malloc( sizeof(FSCAN) );
  41.    if( retbuf ){
  42.       retbuf->findhandle = malloc( sizeof(struct find_t) );
  43.       if(retbuf->findhandle)
  44.          retbuf->sig = FSCAN_SIG;
  45.       else{
  46.          free( retbuf );
  47.          retbuf = NULL;
  48.       }
  49.    }
  50.    return( retbuf );
  51. }
  52.  
  53. void closedir( dh )
  54.    FSCAN *dh;
  55. {
  56.    if( dh->sig != FSCAN_SIG )
  57.       puts( "closedir(): Invalid dir handle");
  58.    else{
  59.       free( dh->findhandle );
  60.       free( dh );
  61.       dh = NULL;
  62.    }
  63. }
  64.  
  65. findfirst( mask, attr, dh )
  66.    char *mask;
  67.    int attr;
  68.    FSCAN *dh;
  69. {
  70.    int retval = -1;
  71.    struct find_t *dta;
  72.    if( dh->sig != FSCAN_SIG )
  73.       puts( "findfirst(): Invalid dir handle");
  74.    else{
  75.       dta = dh->findhandle;
  76.       retval = _dos_findfirst( mask, attr, dta );
  77.       if(!retval){
  78.          strcpy(dh->name, dta->name);
  79.          dh->attrib = dta->attrib;
  80.          dh->wr_time = dta->wr_time;
  81.          dh->wr_date = dta->wr_date;
  82.          dh->size = dta->size;
  83.       }
  84.    }
  85.    return( retval );
  86. }
  87.  
  88. findnext( dh )
  89.    FSCAN *dh;
  90. {
  91.    int retval = -1;
  92.    struct find_t *dta;
  93.  
  94.    if( dh->sig != FSCAN_SIG )
  95.       puts( "findnext(): Invalid dir handle");
  96.    else{
  97.       dta = dh->findhandle;
  98.       retval = _dos_findnext( dta );
  99.       if(!retval){
  100.          strcpy(dh->name, dta->name);
  101.          dh->attrib = dta->attrib;
  102.          dh->wr_time = dta->wr_time;
  103.          dh->wr_date = dta->wr_date;
  104.          dh->size = dta->size;
  105.       }
  106.    }
  107.    return( retval );
  108. }
  109.       
  110. #else  /* OS_2 defined. Assume IBM C/2, and a protect mode only program */
  111.  
  112. #define INCL_DOS
  113. #include <os2.h>
  114.  
  115. FSCAN *opendir(void)
  116. {
  117.    FSCAN *retbuf;
  118.  
  119.    retbuf = malloc( sizeof(FSCAN) );
  120.    if( retbuf ){
  121.       retbuf->findhandle = malloc( sizeof( HDIR ) );
  122.       if(retbuf->findhandle)
  123.          retbuf->sig = FSCAN_SIG;
  124.       else{
  125.          free( retbuf );
  126.          retbuf = NULL;
  127.       }
  128.    }
  129.    return( retbuf );
  130. }
  131.  
  132. void closedir( dh )
  133.    FSCAN *dh;
  134. {
  135.    int stat;
  136.    HDIR *DH;
  137.  
  138.    if( dh->sig != FSCAN_SIG )
  139.       puts( "closedir(): Invalid dir handle");
  140.    else{
  141.       DH = dh->findhandle;
  142.       stat = DosFindClose( *DH );  /* The OS/2 tech ref doesn't make it clear
  143.                                        if you HAVE to do this. */
  144.       free( dh->findhandle );
  145.       free( dh );
  146.       dh = NULL;
  147.    }
  148. }
  149.  
  150. findfirst( mask, attr, dh )
  151.    char *mask;
  152.    int attr;
  153.    FSCAN *dh;
  154. {
  155.    int retval = -1;
  156.    HDIR *DH;
  157.    int count = 1;
  158.    static struct FileFindBuf ffb;
  159.  
  160.    if( dh->sig != FSCAN_SIG )
  161.       puts( "findfirst(): Invalid dir handle");
  162.    else{
  163.       DH = dh->findhandle;
  164.       *DH = 0xffff;      /* request a dir handle */
  165.       retval = DosFindFirst( mask, DH, attr, (PFILEFINDBUF)&ffb, sizeof( ffb ), (PUSHORT)&count, 0l);
  166.       if(!retval){
  167.          strcpy(dh->name, ffb.name);
  168.          dh->attrib = (char)ffb.attrib;
  169.          dh->wr_time = ffb.wr_time;
  170.          dh->wr_date = ffb.wr_date;
  171.          dh->size = ffb.size;
  172.       }
  173.       /*else if( retval != 18 )
  174.          printf("find first error %u\n", retval );*/
  175.    }
  176.    return( retval );
  177. }
  178.  
  179. findnext( dh )
  180.    FSCAN *dh;
  181. {
  182.    int retval = -1;
  183.    HDIR *DH;
  184.    int count = 1;
  185.    static struct FileFindBuf ffb;
  186.  
  187.    if( dh->sig != FSCAN_SIG )
  188.       puts( "findfirst(): Invalid dir handle");
  189.    else{
  190.       DH = dh->findhandle;
  191.       retval = DosFindNext( *DH, (PFILEFINDBUF)&ffb, sizeof( ffb ), (PUSHORT)&count);
  192.       if(!retval){
  193.          strcpy(dh->name, ffb.name);
  194.          dh->attrib = (char)ffb.attrib;
  195.          dh->wr_time = ffb.wr_time;
  196.          dh->wr_date = ffb.wr_date;
  197.          dh->size = ffb.size;
  198.       }
  199.       /*else if( retval != 18 )
  200.          printf("find next error %u\n", retval );*/
  201.    }
  202.    return( retval );
  203. }
  204.       
  205. #endif  /* OS_2 */
  206.  
  207.  
  208. #ifdef TEST_SHELL
  209.  
  210. int walk(char *path);
  211.  
  212. void main(int argc, char **argv)
  213. {
  214.     walk("\\");     // start at root
  215. }
  216.  
  217. /* this simple function assumes the path ALWAYS has an ending backslash */
  218. walk(char *path)
  219. {
  220.     FSCAN *dh;
  221.     int done = FALSE;
  222.     char full[66];
  223.  
  224.     strcpy(full, path);
  225.     strcat(full, "*.*");
  226.     if( dh = opendir() ){
  227.         for( done = findfirst(full, _A_SUBDIR, dh); !done; done = findnext(dh)){
  228.             if( (dh->attrib & _A_SUBDIR) && (dh->name[0] != '.') ){
  229.                 strcpy(full, path);
  230.                 strcat(full, dh->name);
  231.                 puts(full);
  232.                 strcat(full, "\\");
  233.                 if( !walk(full) )
  234.                     return(FALSE);
  235.             }
  236.         }
  237.         closedir(dh);
  238.         return(TRUE);
  239.     }
  240.     else{
  241.         puts("opendir() failed");
  242.     }
  243.     return(FALSE);
  244. }
  245.  
  246. #endif
  247.  
  248.  
  249.